﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Infrastructure;
using MSIACCSL2.Web.WarehouseServices;

namespace MSIACCSL2.Web.ReportPage
{
    public partial class SR : System.Web.UI.Page
    {
        private DataAccess<Object> DA;
        private ReportDocument SalesReturn;
        private ReportDocument SalesReturnDetail;
        private string Tipe;

        public SR()
        {
            DA = new DataAccess<object>();
            SalesReturn = new ReportDocument();
            SalesReturnDetail = new ReportDocument();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["tipe"] = RadioButtonList1.SelectedValue;
            if (IsPostBack)
                if (RadioButtonList2.SelectedValue.ToString().Contains("Summary"))
                    LoadReportSummary(Request.QueryString["no"].ToString(), RadioButtonList1.SelectedValue);
                else
                    LoadReportDetail(Request.QueryString["no"].ToString(), RadioButtonList1.SelectedValue);
        }
      
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (RadioButtonList2.SelectedValue.ToString().Contains("Summary"))
                LoadReportSummary(Request.QueryString["no"].ToString(), ViewState["tipe"].ToString());
            else
                LoadReportDetail(Request.QueryString["no"].ToString(), ViewState["tipe"].ToString());

        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["tipe"] = RadioButtonList1.SelectedValue;
        }


        public void LoadReportSummary(string  NoDO,string tipe)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Select a.*,i.SerialNumber,i.NoBundle,i.QtyPack, ");
            sb.Append("i.Panjang,i.Lebar,i.Tinggi,i.QtyUnit,i.KodeMasuk,iw.KodePallet,i.SatuanPack,i.SatuanUnit, ");
            sb.Append("c.*,it.ItemName,s.SubGroupName,iw.KodeWarehouse, ");
            sb.Append("b.QtyUnit AS Qty ,b.Price,b.Discount AS DiscountD,'Rp' AS DiscountTypeD,c.CustomerName ");
            sb.Append("FROM SalesReturn a  ");
            sb.Append("inner Join SalesReturnDetail b on a.SRNo = b.SRNo  ");
            sb.Append("LEFT JOIN inventory i ON i.SerialNumber = b.SerialNumber  ");
            sb.Append("Left Join Customer c on c.CustomerId = a.CustomerId  ");
            sb.Append("Left Join Item it on it.ItemId = i.ItemId   ");
            sb.Append("Left Join Subgroup s on s.SubGroupID = it.SubgroupId ");
            sb.Append("Left JOIN (Select SerialNumber,KodeWarehouse,Pallet AS KodePallet from uvw_mutasi_stok) iw ON i.SerialNumber = iw.SerialNumber AND iw.KodeWarehouse=a.KodeWarehouse ");
            sb.AppendFormat("Where a.SRNo='{0}' Order By s.SubGroupID,i.ItemId ", NoDO);

            var Source = DA.GetDataListDataSet(sb.ToString());
            string reportPath = Server.MapPath("~/Report/SalesReturn.rpt");
            SalesReturn.Load(reportPath);
            SalesReturn.SetDataSource(Source.Tables[0]);
            var param = new ParameterDiscreteValue();
            param.Value = tipe;
            var paramValue = new ParameterValues();
            paramValue.Add(param);
            SalesReturn.DataDefinition.ParameterFields["group"].ApplyCurrentValues(paramValue);
            CrystalReportViewer1.ReportSource = SalesReturn;
        }

        public void LoadReportDetail(string NoDO,string tipe)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Select a.*,i.SerialNumber,i.NoBundle,i.QtyPack, ");
            sb.Append("i.Panjang,i.Lebar,i.Tinggi,i.QtyUnit,i.KodeMasuk,iw.KodePallet,i.SatuanPack,i.SatuanUnit, ");
            sb.Append("c.*,it.ItemName,s.SubGroupName,iw.KodeWarehouse, ");
            sb.Append("b.QtyUnit AS Qty ,b.Price,b.Discount AS DiscountD,'Rp' AS DiscountTypeD,c.CustomerName ");
            sb.Append("FROM SalesReturn a  ");
            sb.Append("inner Join SalesReturnDetail b on a.SRNo = b.SRNo  ");
            sb.Append("LEFT JOIN inventory i ON i.SerialNumber = b.SerialNumber  ");
            sb.Append("Left Join Customer c on c.CustomerId = a.CustomerId  ");
            sb.Append("Left Join Item it on it.ItemId = i.ItemId   ");
            sb.Append("Left Join Subgroup s on s.SubGroupID = it.SubgroupId ");
            sb.Append("Left JOIN (Select SerialNumber,KodeWarehouse,Pallet AS KodePallet from uvw_mutasi_stok) iw ON i.SerialNumber = iw.SerialNumber AND iw.KodeWarehouse=a.KodeWarehouse ");
            sb.AppendFormat(" Where a.SRNo='{0}'  Order By s.SubGroupID,i.ItemId  ", NoDO);

            var Source = DA.GetDataListDataSet(sb.ToString());
            string reportPath = Server.MapPath("~/Report/SalesReturnDetail.rpt");
            SalesReturnDetail.Load(reportPath);
            SalesReturnDetail.SetDataSource(Source.Tables[0]);
            var param = new ParameterDiscreteValue();
            param.Value = tipe;
            var paramValue = new ParameterValues();
            paramValue.Add(param);
            SalesReturnDetail.DataDefinition.ParameterFields["group"].ApplyCurrentValues(paramValue);
            CrystalReportViewer1.ReportSource = SalesReturnDetail;
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            this.Unload += Page_Unload;
        }

        void Page_Unload(object sender, EventArgs e)
        {
            SalesReturn.Clone();
            SalesReturn.Dispose();

            SalesReturnDetail.Clone();
            SalesReturnDetail.Dispose();
        }
    }
}